//
//  ZHTabBarView.m
//  中国福利彩票(全部)
//
//  Created by 李智恒 on 16/3/8.
//  Copyright © 2016年 Zhli. All rights reserved.
//

#import "NBTabBarView.h"

@interface NBTabBarView ()

@property (nonatomic,weak) UIButton *selBtn;

@end

@implementation NBTabBarView

-(void)setItems:(NSArray *)items
{
    _items=items;
    
    for (int i=0; i<items.count; i++) {
        
        UITabBarItem *item=items[i];
        
        UIButton *btn=[UIButton buttonWithType:(UIButtonTypeCustom)];
        
        btn.tag=i;
        
        [btn setImage:item.image  forState:(UIControlStateNormal)];
        
        [btn setImage:item.selectedImage forState:(UIControlStateSelected)];
        
        [btn addTarget:self action:@selector(TabBarBtnClick:) forControlEvents:(UIControlEventTouchDown)];
        
        if (i==0) {
            [self TabBarBtnClick:btn];
        }
        
        [self addSubview:btn];
        
        
    }
    
}

//点击按钮事件
-(void)TabBarBtnClick:(UIButton *)btn
{
    self.selBtn.selected=NO;
    
    btn.selected=YES;
    
    self.selBtn=btn;

    //通知代理点击了按钮
    if ([self.delegate respondsToSelector:@selector(tabbar:btnClickIndex:)]) {
        [self.delegate tabbar:self btnClickIndex:self.selBtn.tag];
    }
    
}

//设置按钮尺寸
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat btnX=0;
    CGFloat btnY=0;
    CGFloat btnW=[UIScreen mainScreen].bounds.size.width/self.subviews.count;
    CGFloat btnH=self.frame.size.height;
    
    for (int i=0;i<self.subviews.count;i++) {
        
        btnX=i*btnW;
        
        UIButton *btn=self.subviews[i];
        
        btn.frame=CGRectMake(btnX, btnY, btnW, btnH);
        
    }

}

@end
